<?php


namespace App\Http\Admin\Controllers;

use Illuminate\Http\Request;
use App\Http\Admin\Controllers\Controller as Controller;
use Illuminate\Support\Facades\DB;

/*
 *  案件表接口
 *  @author 小林《1814992057@qq.com》
 *  @datetime   2019/8/23
*/

class  LawyerOfficeController extends Controller
{

    /*
     * 添加律师所
     * @return json
     * */
    public function add_lawyer_office(Request $request){

        $data=$request->only('name','status','description');
        if(!preg_match('/^[\w\x{4e00}-\x{9fa5}]{2,20}$/u',$data['name'])) return ThrowApiExc('20031');
        if(!preg_match('/^[0-9]{1,2}$/u',$data['status'])) return ThrowApiExc('200032');

        $se = DB::table('C_lawyerOffice/')
            ->where([
                "name"=>$data['name']
            ])
            ->first();
        if($se){
            return [
                'status_code'      => 41825,
                'message'     =>  '姓名重复',
                'data' => ""
            ];
        }

        $res = DB::table('C_lawyerOffice')->insertGetId(
            [
                'name'=>$data['name'],
                'status'=>$data['status']
            ]
        );

        if($res){
            return [
                'status_code'      => 200,
                'message'     =>  '提交成功',
                'data' => ['id'=>$res]
            ];
        }else{
            return ThrowApiExc('40002');
        }



    }

    /*
     * 修改律师所
     * @return json
     * */

    public function update_lawyer_office(Request $request,$loid){

        $data=$request->only('name','status','description');
        if(!preg_match('/^[\w\x{4e00}-\x{9fa5}]{2,20}$/u',$data['name'])) return ThrowApiExc('20031');
        if(!preg_match('/^[0-9]{1,2}$/u',$data['status'])) return ThrowApiExc('200032');

        $res = DB::table('C_lawyerOffice')->where("id",$loid)->update(
            [
                'name'=>$data['name'],
                'status'=>$data['status']
            ]
        );

        if($res){
            return [
                'status_code'      => 200,
                'message'     =>  '提交成功',
                'data' => ['id'=>$res]
            ];
        }else{
            return ThrowApiExc('40002');
        }


    }

    /*
     * 删除律师所
     * @return json
     * */


    public function delete_lawyer_office($loid){


        $res=DB::table('C_lawyerOffice')->where('id',$loid)->delete();

        if($res){
            return [
                'status_code'      => 200,
                'message'     =>  '删除成功',
                'data' => []
            ];
        }else{
            return ThrowApiExc('40003');
        }
    }


    /*
     * 查找律师所
     * @return json
     * */


    public function select_lawyer_office(){

        $data=DB::table('C_lawyerOffice')->get();

        return [
            'status_code'      => 200,
            'message'     =>  '请求成功',
            'data' => $data
        ];


    }

    public function select_lawyer_office_case(Request $request){
        $data=$request->only('id','title');
        $data=DB::table('C_lawyerOffice')->where($data)->get();

        return [
            'status_code'      => 200,
            'message'     =>  '请求成功',
            'data' => $data
        ];
    }



}